package com.zhuiYun.project.api.handworkdiy.service.impl;

import com.alipay.api.domain.LogisticsCompanyIstd;
import com.zhuiYun.project.api.commoditydetail.mapper.CommodityMapper;
import com.zhuiYun.project.api.handworkdiy.entity.CommodityAndDiyVo;
import com.zhuiYun.project.api.handworkdiy.entity.HandworkDiy;
import com.zhuiYun.project.api.handworkdiy.entity.HandworkDiyVo;
import com.zhuiYun.project.api.handworkdiy.mapper.HandworkDiyMapper;
import com.zhuiYun.project.api.handworkdiy.service.HandworkService;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HandworkServiceImpl
 * @Description TODO
 * @Author lzy
 * @Date 2023/4/5 17:49
 **/
@Service
public class HandworkServiceImpl implements HandworkService {

    @Autowired
    HandworkDiyMapper handworkMapper;
    @Autowired
    CommodityMapper commodityMapper;

    /**
     * @param ids
     * @Description 获取手工diy信息列表
     * @Author lzy
     * @Date 2023/4/5 19:49
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getHandworkDiyListByIds(String ids) throws CommonException {
        String[] diyIds = ids.split(",");
        Example example = new Example(HandworkDiy.class);
        Example.Criteria criteria = example.createCriteria();
        for (String diyId : diyIds) {
            criteria.orEqualTo("id", diyId);
        }
        criteria.andEqualTo("delFlag", "0");
        List<HandworkDiy> handworkDiys = handworkMapper.selectByExample(example);
        return CommonResult.ok(handworkDiys);
    }

    /**
     * @param
     * @Author kdj
     * @Description 查询DIY的特价商品方法
     * @Date 2023/4/5 19:18
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getHandworkDiyListByOffer() {
        List<HandworkDiy> list = handworkMapper.getHandworkDiyListByOffer();
        return CommonResult.ok(list);
    }

    /**
     * @param
     * @Author kdj
     * @Description 查询DIY的特色商品方法
     * @Date 2023/4/5 21:02
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getHandworkDiyListByCharacteristic() {
        List<HandworkDiy> list = handworkMapper.getHandworkDiyListByCharacteristic();
        return CommonResult.ok(list);
    }

    /**
     * @param
     * @Author kdj
     * @Description 查询DIY的类型列表
     * @Date 2023/4/6 12:01
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getHandworkDiyTypeList() {
        List<Map> list = handworkMapper.getHandworkDiyTypeList();
        return CommonResult.ok(list);
    }

    /**
     * @param typeId
     * @param queryDiyName
     * @param minPrice
     * @param maxPrice
     * @param orderValue
     * @param pageRequest
     * @Author kdj
     * @Description 根据Pc页面条件查询Diy商品列表
     * @Date 2023/4/7 11:48
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getHandworkDiyListByPageParams(Integer typeId, String queryDiyName, Integer minPrice,
                                                       Integer maxPrice, String orderValue,
                                                       PageRequest pageRequest) {
        //开始分页
        MybatisPageHelper.startPage(pageRequest);
        List<Map> list = handworkMapper.getHandworkDiyListByPageParams(typeId, queryDiyName, minPrice, maxPrice, orderValue);
        return CommonResult.ok(MybatisPageHelper.getPageResult(list));
    }


    /**
     * @param id
     * @Author kdj
     * @Description 根据diyid获取 DIY商品信息
     * @Date 2023/4/7 14:38
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getHandworkDiyById(Integer id) {
        HandworkDiyVo handworkDiyVo = handworkMapper.getHandworkDiyById(id);
        return CommonResult.ok(handworkDiyVo);
    }

    /**
     * @param id
     * @Author kdj
     * @Description 计算diy商品评论总数量
     * @Date 2023/4/7 17:10
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult calculateTheTotalNumberOfComments(Integer id) {
        int num = handworkMapper.calculateTheTotalNumberOfComments(id);
        return CommonResult.ok(num);
    }

    /**
     * @param pageRequest
     * @param id
     * @Author kdj
     * @Description 根据diyid查看diy商品所有评论
     * @Date 2023/4/7 17:46
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult queryAllComments(Integer id, PageRequest pageRequest) {
        //开始分页
        MybatisPageHelper.startPage(pageRequest);
        List<Map<String, Object>> list = handworkMapper.queryAllComments(id);
        //判断是否有评论
        if (list.size() == 0) {
            return CommonResult.ok("暂无评论");
        } else {
            //改变createTime格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            for (Map<String, Object> row : list) {
                String createTime = row.get("createTime").toString();
                String s = createTime.substring(0, createTime.indexOf("T")) + " " + createTime.substring(createTime.indexOf("T") + 1, createTime.length());
                row.put("createTime", s);
            }
            return CommonResult.ok(MybatisPageHelper.getPageResult(list));
        }
    }
    /**
     *
     * @Author lzk
     * @Description 查询diy所有信息
     * @date 2023/4/12 15:19
     * Return com.zhuiYun.project.common.response.CommonResult
     */
    @Override
    public CommonResult queryDIYInformation() {
        List<HandworkDiy> list = handworkMapper.selectAll();
        return CommonResult.ok(list);
    }

    /**
     * @Author gpd
     * @Description 搜索栏模糊查（uniapp首页搜索栏）
     * @Date 2023-04-13 上午 08:45
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param selectValue
     */
    @Override
    public List<CommodityAndDiyVo> selectCommodity(String selectValue) {
        List<CommodityAndDiyVo> listAll = new ArrayList<>();
        if(selectValue == ""){
            return listAll;
        }
        List<CommodityAndDiyVo> diyList = handworkMapper.getDiyCommodityName(selectValue);
        List<CommodityAndDiyVo> commodityList = commodityMapper.getCommodityName(selectValue);
        listAll.addAll(diyList);
        listAll.addAll(commodityList);
        return listAll;
    }
}
